package logic

import (
	"book/book_srv/internal/svc"
	"book/book_srv/pb"
	"book/model/mysql"
	"context"
	"github.com/zeromicro/go-zero/core/logx"
)

type BookListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBookListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BookListLogic {
	return &BookListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BookListLogic) BookList(in *pb.BookListReq) (*pb.BookListResp, error) {
	book := mysql.Book{
		Name: in.Name,
	}
	list, err := book.BookList()
	if err != nil {
		logx.Error("查询信息有误")
		return nil, err
	}
	var bookInfo []*pb.BookInfo
	for _, v := range list {
		bookInfo = append(bookInfo, &pb.BookInfo{
			BookId: int64(v.ID),
			Name:   v.Name,
		})
	}
	return &pb.BookListResp{BookInfo: bookInfo}, nil
}
